<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Initialize a secretstream context for encryption</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.sodium-crypto-secretstream-xchacha20poly1305-init-pull.html">« sodium_crypto_secretstream_xchacha20poly1305_init_pull</a></li>
      <li style="float: right;"><a href="function.sodium-crypto-secretstream-xchacha20poly1305-keygen.html">sodium_crypto_secretstream_xchacha20poly1305_keygen »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.sodium.html">Sodium 函数</a></li>
    <li>Initialize a secretstream context for encryption</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.sodium-crypto-secretstream-xchacha20poly1305-init-push" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">sodium_crypto_secretstream_xchacha20poly1305_init_push</h1>
  <p class="verinfo">(PHP 7 &gt;= 7.2.0, PHP 8)</p><p class="refpurpose"><span class="refname">sodium_crypto_secretstream_xchacha20poly1305_init_push</span> &mdash; <span class="dc-title">Initialize a secretstream context for encryption</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-function.sodium-crypto-secretstream-xchacha20poly1305-init-push-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>sodium_crypto_secretstream_xchacha20poly1305_init_push</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$key</code></span>): <span class="type">array</span></div>

  <p class="para rdfs-comment">
   Initialize a secretstream context for encryption.
  </p>

 </div>


 <div class="refsect1 parameters" id="refsect1-function.sodium-crypto-secretstream-xchacha20poly1305-init-push-parameters">
  <h3 class="title">参数</h3>
  <dl>
   
    <dt>
<code class="parameter">key</code></dt>

    <dd>

     <p class="para">
      Cryptography key. See <span class="function"><a href="function.sodium-crypto-secretstream-xchacha20poly1305-keygen.html" class="function">sodium_crypto_secretstream_xchacha20poly1305_keygen()</a></span>.
     </p>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.sodium-crypto-secretstream-xchacha20poly1305-init-push-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   An array with two string values:
  </p>
  <ul class="simplelist">
   <li class="member">The secretstream state, needed for further pushes</li>
   <li class="member">The secretstream header, which needs to be provided to the recipient so they can pull data</li>
  </ul>
 </div>


 <div class="refsect1 examples" id="refsect1-function.sodium-crypto-secretstream-xchacha20poly1305-init-push-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="sodium-crypto-secretstream-xchacha20poly1305-init-push.example.basic">
   <p><strong>示例 #1 <span class="function"><strong>sodium_crypto_secretstream_xchacha20poly1305_init_push()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">encrypt_file</span><span style="color: #007700">(</span><span style="color: #0000BB">string&nbsp;$inputFilePath</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">string&nbsp;$outputFilePath</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">string&nbsp;$key</span><span style="color: #007700">):&nbsp;</span><span style="color: #0000BB">void<br /></span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;[</span><span style="color: #0000BB">$state</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$header</span><span style="color: #007700">]&nbsp;=&nbsp;</span><span style="color: #0000BB">sodium_crypto_secretstream_xchacha20poly1305_init_push</span><span style="color: #007700">(</span><span style="color: #0000BB">$key</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$inputFile&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #0000BB">$inputFilePath</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'rb'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$outputFile&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fopen</span><span style="color: #007700">(</span><span style="color: #0000BB">$outputFilePath</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'wb'</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Write&nbsp;the&nbsp;header:<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$outputFile</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$header</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$inputFileSize&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fstat</span><span style="color: #007700">(</span><span style="color: #0000BB">$inputFile</span><span style="color: #007700">)[</span><span style="color: #DD0000">'size'</span><span style="color: #007700">];<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">//&nbsp;Encrypt&nbsp;the&nbsp;file&nbsp;and&nbsp;write&nbsp;its&nbsp;contents&nbsp;to&nbsp;the&nbsp;output&nbsp;file:<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">for&nbsp;(</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">$inputFileSize</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">+=&nbsp;</span><span style="color: #0000BB">8175</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$ptxt_chunk&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">fread</span><span style="color: #007700">(</span><span style="color: #0000BB">$inputFile</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">8175</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$ctxt_chunk&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">sodium_crypto_secretstream_xchacha20poly1305_push</span><span style="color: #007700">(</span><span style="color: #0000BB">$state</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$ptxt_chunk</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$outputFile</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$ctxt_chunk</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">sodium_memzero</span><span style="color: #007700">(</span><span style="color: #0000BB">$state</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$inputFile</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$outputFile</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #FF8000">//&nbsp;sodium_crypto_secretstream_xchacha20poly1305_keygen()<br /></span><span style="color: #0000BB">$key&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">sodium_base642bin</span><span style="color: #007700">(</span><span style="color: #DD0000">'MS0lzb7HC+thY6jY01pkTE/cwsQxnRq0/2L1eL4Hxn8='</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">SODIUM_BASE64_VARIANT_ORIGINAL</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">file_put_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'hello.txt'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Hello&nbsp;world!'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">encrypt_file</span><span style="color: #007700">(</span><span style="color: #DD0000">'hello.txt'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'hello.txt.encrypted'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$key</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">sodium_bin2hex</span><span style="color: #007700">(</span><span style="color: #0000BB">file_get_contents</span><span style="color: #007700">(</span><span style="color: #DD0000">'hello.txt.encrypted'</span><span style="color: #007700">)));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
string(106) &quot;971e33b255f0990ef3931caf761c59136efa77b434832f28ec719e3ff73f5aec38b3bba1574ab5b70a8844d8da36a668e802cfea2c&quot;
</pre></div>
   </div>
  </div>
 </div>


</div></div></div></body></html>